const app = getApp();
import {
  wxPay,
  generate,
  addressList,
  goodsDetail
} from "../../../server/api";
Page({
  data: {
    oss_url: app.globalData.oss_url,
    userInfo:{},
    goodsId:'',
    adsList:[],   //收货地址
    addresStatus:0,
    goodsObj:{},
    addressStr:'',
    bigImg:'',
    payPrice:'',   //实付金额
    receiverInfo:'',   //收货地址信息
    customName:'',
    customPhone:'',
    customSex:0,
    giftsId:'',
    totalAmount:'',
    payAmount:'',
    giftsAmount:'',
    receiverId:'',
    goodsId:'',
    goodsName:'',
    goodsPrice:'',
    goodsQuantity:1,
    remark:''
  },
  onLoad: function (options) {
    this.setData({
      goodsId:options.id
    })
    this.detail()
    this.getAddressList()
  },
  onShow(){
   let that=this;
   wx.getStorage({
    key: 'user_info',
    success (res) {
      console.log(res.data)
        that.setData({
          userInfo:res.data
        })
      }
   })
  },
  detail(){
    let data={
      id:this.data.goodsId
    }
    goodsDetail(data).then(res=>{
      if(res.code==0){
        //实付金额
        let price=res.data.priceAtg-res.data.priceDeduction;
        //商品图片
        let pics=res.data.goodsPic.split(',');
        this.setData({
          goodsObj:res.data,
          bigImg:pics[0],
          payPrice:price
        })
      }
    })
  },
  getAddressList(){
    let data={
       oneId: app.globalData.user_info.oneId
    }
    addressList(data).then(res=>{
      if(res.code==0){
        let list =res.data;
        if(list.length>0){
          list.forEach((item)=>{  
            let addstr=item.receiverProvince+item.receiverCity+item.receiverArea+item.receiverDetailAddress;
          //  if(item.defaultFlag==1){
              this.setData({
                addressObj:item,
                addresStatus:1,
                addressStr:addstr,
                receiverId:item.receiverId
              })
          //  }
          })
        }
      }
    })
  },
  submit(){
    let data={
      appId:'wxe4299f541f78a49e',
      oneId:app.globalData.user_info.oneId,
      openId:app.globalData.user_info.openId,
      customName:this.data.userInfo.nickName,
      customPhone:Number(app.globalData.user_info.phone),
      customSex:this.data.userInfo.gender,
      giftsId:this.data.giftsId,
      totalAmount:this.data.payPrice,
      // payAmount:this.data.payPrice,
      payAmount:0.01,
      giftsAmount:this.data.goodsObj.priceDeduction,
      receiverId:2,
      goodsId:this.data.goodsId,
      goodsName:this.data.goodsObj.goodsName,
      goodsPrice:this.data.goodsObj.priceAtg,
      goodsQuantity:1,
      remark:this.data.remark
    }
    console.log(data)
    generate(data).then(res=>{
      if(res.code==0){
        this.goPay(res)
      }
    })
  },
  getRemark(e){
    this.setData({
      remark:e.detail.value
    })
  },
  goPay(res){
    wx.showToast({
      title: '支付请求中',
      icon: 'loading',
      duration: 3000
    })
    wx.requestPayment(
      {
        'timeStamp': res.data.timeStamp,
        'nonceStr': res.data.nonceStr,
        'package': res.data.package,
        'signType': res.data.signType,
        'paySign': res.data.paySign,
        'success': function (res) {
          console.log(res);
          wx.showToast({
            title: '支付成功',
            icon: 'none',
          })
          // 成功之后跳转-我的订单 页
          wx.navigateTo({
            url: "/pages/goodsOrder/orderList/index",
          })
        },
        'fail': function (res) {
          wx.showLoading({ title: "支付错误,请重新支付" });
          setTimeout(function () { wx.hideLoading() }, 1000);
        },
        'complete': function (res) {
          setTimeout(function () { wx.hideLoading() }, 1000);
        }
      })
  }
})